package com.kevin.action.back;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.filter.CheckSession;
import org.nutz.mvc.view.JspView;

import com.kevin.common.DwzoutMapUtil;
import com.kevin.common.PassUtil;
import com.kevin.common.RequestParamUtil;
import com.kevin.dao.ManagerDao;
import com.kevin.model.Article;
import com.kevin.model.Manager;
import com.kevin.model.UserCalender;
import com.kevin.util.DateConvertUtil;
@IocBean
@At("/admin/user")
@Filters( { @By(type = CheckSession.class, args = { "admin", "/admin/loginp.shtml" }) })
public class UserAction {
	@Inject
	private ManagerDao managerDao;
	
	/**
	 * 根据id修改密码
	 * 
	 * @param id
	 * @param ioc
	 * @return
	 */
	@At()
	@Ok("json")
	@Fail("json")
	public Map<String, Object> updatepwd(@Param("::user.") Manager user,
							@Param("oldpwd") String oldpwd,
							HttpServletRequest req) {
		Map<String, Object> returnMap = DwzoutMapUtil.getErrDwzoutMap("密码修改失败！");
		Manager man = managerDao.find(user.getId(), Manager.class);
		if (!man.getPassword().equals(oldpwd)) {
			return returnMap;
		}

		user.setState(true);
		user.setPassword(PassUtil.encryptPass(user.getPassword()));
		boolean flag = managerDao.update(user);
		if(flag)
			returnMap = DwzoutMapUtil.getSuccDwzoutMap("密码修改成功！");
		return returnMap;
	}
	//======================================我的日历管理begin==========================================
	
	/**
	 * 获取日历信息
	 * 
	 * @param id
	 * @param ioc
	 * @return
	 */
	@At()
	public View calendar(HttpServletRequest req,HttpSession session) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		if(req.getParameter("y") == null){
			try {
				calendar.set(Calendar.YEAR, Integer.parseInt(req.getParameter("y")));
			} catch (NumberFormatException e) {}
		}
		if(req.getParameter("m") == null){
			try {
				calendar.set(Calendar.MONTH, Integer.parseInt(req.getParameter("m")));
			} catch (NumberFormatException e) {}
		}
		req.setAttribute("y", calendar.get(Calendar.YEAR));
		req.setAttribute("m", calendar.get(Calendar.MONTH));
		calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一周的第一天
		calendar.get(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.WEEK_OF_MONTH,1);//
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		Date startDate = calendar.getTime();//当前月份第一周第一天
		//System.out.println(startDate.toLocaleString()+" =="+calendar.get(Calendar.WEEK_OF_MONTH));
		calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置周一为一周的第一天
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH)+1);  
		calendar.set(Calendar.DAY_OF_MONTH, 1);  
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		Date endDate = calendar.getTime();//当前月份最后一周最后一天
		//System.out.println(endDate.toLocaleString()+" =="+calendar.get(Calendar.WEEK_OF_MONTH));
		
		SqlExpressionGroup group = RequestParamUtil.dealRequest(req);

		Manager man = (Manager)session.getAttribute("admin");
		group = group.and("userid", "=", man.getId());
		if(req.getParameter("date_sd") == null && req.getParameter("date_ed") == null){
			group = group.and("start", ">", startDate);
			group = group.and("start", "<", endDate);
		}
		req.setAttribute("startdate", DateConvertUtil.convertDate(startDate,"yyyy-MM-dd"));
		req.setAttribute("enddate", DateConvertUtil.convertDate(endDate,"yyyy-M-dd"));
		System.out.println(DateConvertUtil.convertDate(startDate,"yyyy-MM-dd"));
		
		List<UserCalender> uc = managerDao.search(UserCalender.class,Cnd.where(group).desc("start"));	
		System.out.println(Json.toJson(uc));
		req.setAttribute("data", Json.toJson(uc)+"");
		return new JspView("/jsp/back/user/calendar");
	}
	/**
	 * 获取日历信息
	 * 
	 * @param id
	 * @param ioc
	 * @return
	 */
	@At()
	@Ok("json")
	@Fail("json")
	public List<UserCalender> getCalendarInfo(HttpServletRequest req,HttpSession session) {
		
		
		SqlExpressionGroup group = RequestParamUtil.dealRequest(req);

		Manager man = (Manager)session.getAttribute("admin");
		SqlExpressionGroup useridsql = Cnd.exps("userid", "=", man.getId()).or("userid", "=", "-1");
		group = group.and(useridsql);
		String sstarttime = req.getParameter("sstarttime");
		String estarttime = req.getParameter("estarttime");
		SqlExpressionGroup sdate = Cnd.exps("starttime", ">", DateConvertUtil.convertDate(sstarttime,"yyyy-MM-dd")).and("starttime", "<", DateConvertUtil.convertDate(estarttime,"yyyy-MM-dd"));
		SqlExpressionGroup edate = Cnd.exps("endtime", ">", DateConvertUtil.convertDate(sstarttime,"yyyy-MM-dd")).and("endtime", "<", DateConvertUtil.convertDate(estarttime,"yyyy-MM-dd"));
		SqlExpressionGroup date = Cnd.exps(sdate).or(edate);
		group = group.and(date);
		
		List<UserCalender> uc = managerDao.search(UserCalender.class,Cnd.where(group).desc("start"));	
//		System.out.println(Json.toJson(uc));
		return uc;
	}
	/**
	 * 获取日历信息 
	 * @param id
	 * @param ioc
	 * @return
	 */
	@At()
	@Ok("json")
	@Fail("json")
	public Map<String, Object> updateCalendarInfo(int id,HttpServletRequest req,HttpSession session) {		
		UserCalender uc = managerDao.find(id,UserCalender.class);
		
		Map<String, Object> returnMap = DwzoutMapUtil.getErrDwzoutMap("日历信息修改失败！");
		Boolean flag = false;
		String end = req.getParameter("end");
		String start = req.getParameter("start");
		uc.setStart(DateConvertUtil.convertDate(start, "yyyy-MM-dd hh:mm:ss"));
		if(!end.equals("null")){
			uc.setEnd(DateConvertUtil.convertDate(end, "yyyy-MM-dd hh:mm:ss"));
			if(end.equals("")){
				uc.setEnd(DateConvertUtil.convertDate(start, "yyyy-MM-dd hh:mm:ss"));
			}
		}
		try {
			if(managerDao.update(uc)){
				flag = true;
				returnMap = DwzoutMapUtil.getSuccDwzoutMap("日历信息修改成功！");
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
		returnMap.put("flag", flag);
		System.out.println(id+req.getParameter("start"));
		System.out.println("end=="+end);
		return returnMap;
	}
	//======================================我的日历管理end==========================================
}
